package cn.pconline.autoclub.web;

import cn.pconline.autoclub.service.CensorDynaInfoService;
import cn.pconline.autoclub.service.CensorDynaReplyService;
import cn.pconline.autoclub.service.CensorIntroduceService;
import cn.pconline.autoclub.service.CensorNoticeService;
import cn.pconline.autoclub.service.CensorPhotoReplyService;
import cn.pconline.autoclub.service.CensorService;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author chensy
 */
@Controller
@RequestMapping("/censor")
public class CensorController {

    @Autowired
    private CensorNoticeService censorNoticeService;
    @Autowired
    private CensorIntroduceService censorIntroduceService;
    @Autowired
    private CensorDynaReplyService censorDynaReplyService;
    @Autowired
    private CensorDynaInfoService censorDynaInfoService;
    @Autowired
    private CensorPhotoReplyService censorPhotoReplyService;
    
    protected static final Logger log = LoggerFactory.getLogger(CensorController.class);

    /**
     * 审核的反馈
     *
     * @param request
     */
    @RequestMapping("/feedback.do")
    @ResponseBody
    public void feedback(HttpServletRequest request) {
        String interfaceUrl = (String) request.getAttribute("HTTPSQS");
        String json = request.getParameter("msg");
        if (json == null || "".equals(json)) {
            log.error("msg is null or is ''!");
            return;
        }
        JSONObject params = JSONObject.parseObject(json);
        params = JSONObject.parseObject(params.get("msg").toString());
        String url = (String) params.get("url");
        String versionStr = (String) params.get("version");
        String statusStr = (String) params.get("status");
        //String updateAt = (String) params.get("updateAt");
        int status = Integer.parseInt(statusStr);
        long version = Long.parseLong(versionStr);
        //Date updateTime = new Date(Long.parseLong(updateAt));
        if (CensorService.isCommonClub(url)) { //普通车友会
            if (censorNoticeService.isCorrectFeedback(url)) {
                censorNoticeService.feedback(interfaceUrl, url, status, version);
            } else if (censorIntroduceService.isCorrectFeedback(url)) {
                censorIntroduceService.feedback(interfaceUrl, url, status, version);
            }
        } else {//官方车友会
            if (censorNoticeService.isCorrectFeedback(url)) {
                censorNoticeService.feedbackOfficialClub(interfaceUrl, url, status, version);
            } else if (censorIntroduceService.isCorrectFeedback(url)) {
                censorIntroduceService.feedbackOfficialClub(interfaceUrl, url, status, version);
            }
        }
        //动态相关
        if (censorDynaInfoService.isCorrectFeedback(url)) {
            censorDynaInfoService.feedback(interfaceUrl, url, status, version);
        } else if (censorDynaReplyService.isCorrectFeedback(url)) {
            censorDynaReplyService.feedback(interfaceUrl, url, status, version);
        } else if(censorPhotoReplyService.isCorrectFeedback(url)){
        	censorPhotoReplyService.feedback( interfaceUrl, url, status, version );
        }
    }
}
